package bookshop.controller.front;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bookshop.domain.Book;
import bookshop.domain.CartItem;
import bookshop.domain.ShoppingCart;
import bookshop.service.BookService;
import bookshop.service.ShoppingCartService;
import bookshop.service.impl.BookServiceImpl;
import bookshop.service.impl.ShoppingCartServiceImpl;

public class ShoppingCartServlet extends HttpServlet {



	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		String act = request.getParameter("act");
		
		
          if("addBookToCart".equals(act)){
			
			//find or create a shoppingcart in session scope, key=shoppingcart
			ShoppingCart cart=(ShoppingCart)request.getSession().getAttribute("shoppingcart");
			String bookCode=request.getParameter("bookcode");
			if(cart==null)
			{
				cart=new ShoppingCart();
			}
			
			//load the book info
			BookService bookService=new BookServiceImpl();
			Book book=bookService.getBookByCode(bookCode);	
			
			ShoppingCartService cartService=new ShoppingCartServiceImpl();
			cartService.addBookToCart(book, cart);
			
			request.getSession().setAttribute("shoppingcart", cart);
			response.sendRedirect("shoppingcart?act=showShoppingCart");
			
		}else if("removeBookInCart".equals(act)){
			String bookCode = request.getParameter("bookcode");
			ShoppingCart cart=(ShoppingCart)request.getSession().getAttribute("shoppingcart");
			
			ShoppingCartService cartService=new ShoppingCartServiceImpl();
			cartService.delBookInCart(bookCode, cart);
			
			response.sendRedirect("shoppingcart?act=showShoppingCart");
			
		}else if("updateShoppingCart".equals(act)){
			
			String amount = request.getParameter("type");
			String bookCode = request.getParameter("bookCode");
			ShoppingCart cart=(ShoppingCart)request.getSession().getAttribute("shoppingcart");
			ShoppingCartService cartService = new ShoppingCartServiceImpl();
			CartItem item = cartService.getItemByBookCode(bookCode, cart);
			if(amount.equals("add")){
			    item.setAmount(item.getAmount()+1);
			}else if(amount.equals("sub")){
				item.setAmount(item.getAmount()-1);
			}else {
				
				item.setAmount(Integer.parseInt(amount));
			}
			cartService.updateBookInCart(bookCode, item, cart);
			
			response.sendRedirect("shoppingcart?act=showShoppingCart");
			
			
		}
		else if("showShoppingCart".equals(act)){
			
			request.getRequestDispatcher("jsps/front/order/show_shoppingcart.jsp").forward(request, response);
		}

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request, response);
	}


}
